﻿Imports ACompany.OrderProcessing.Presentation
Imports System

Partial Class UserControls_Product_ProductDetail
    Inherits System.Web.UI.UserControl
    Implements IProductDetailView

    Private _Presenter As ProductPresenter

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        _Presenter = New ProductPresenter(Me)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            _Presenter.Display()
        End If
    End Sub

    Public Property Name() As String Implements ACompany.OrderProcessing.Presentation.IProductDetailView.Name
        Get
            Return Me.txtName.Text
        End Get
        Set(ByVal value As String)
            Me.txtName.Text = value
        End Set
    End Property

    Public Property Price() As Decimal Implements ACompany.OrderProcessing.Presentation.IProductDetailView.Price
        Get
            Dim decPrice As Decimal
            Decimal.TryParse(Me.txtPrice.Text, decPrice)
            Return decPrice
        End Get
        Set(ByVal value As Decimal)
            Me.txtPrice.Text = value
        End Set
    End Property

    Public Property Stock() As Integer Implements ACompany.OrderProcessing.Presentation.IProductDetailView.Stock
        Get
            Dim intStock As Integer
            Int32.TryParse(Me.txtStock.Text(), intStock)
            Return intStock
        End Get
        Set(ByVal value As Integer)
            Me.txtStock.Text = value
        End Set
    End Property

    Public Sub ErrorMessage(ByVal Message As String) Implements ACompany.OrderProcessing.Presentation.IBaseView.ErrorMessage
        If String.IsNullOrEmpty(Message) Then
            pnlError.Visible = False
        Else
            pnlError.Visible = True
        End If
        Me.litErrorMessage.Text = Replace(Message, Environment.NewLine, "<br/>")
    End Sub

    Public Sub InfoMessage(ByVal Message As String) Implements ACompany.OrderProcessing.Presentation.IBaseView.InfoMessage
        If String.IsNullOrEmpty(Message) Then
            pnlInfo.Visible = False
        Else
            pnlInfo.Visible = True
        End If
        Me.litInfoMessage.Text = Replace(Message, Environment.NewLine, "<br/>")
    End Sub

    Public Sub ViewTitle(ByVal title As String) Implements ACompany.OrderProcessing.Presentation.IBaseView.ViewTitle
        Me.litTitle.Text = title
    End Sub

    Protected Sub btnSaveProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveProduct.Click
        _Presenter.SaveProduct()
    End Sub

    Public ReadOnly Property ProductId() As Guid Implements ACompany.OrderProcessing.Presentation.IProductDetailView.ProductId
        Get
            Dim SelectedProductId As Guid

            If String.IsNullOrEmpty(Request.QueryString("ProductId")) Then
                SelectedProductId = Guid.Empty
            Else
                SelectedProductId = New Guid(Request.QueryString("ProductId"))
            End If

            Return SelectedProductId
        End Get
    End Property

    Public WriteOnly Property SaveButtonText() As String Implements ACompany.OrderProcessing.Presentation.IProductDetailView.SaveButtonText
        Set(ByVal value As String)
            Me.btnSaveProduct.Text = value
        End Set
    End Property

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("ProductList.aspx")
    End Sub
End Class
